package com.magenta.maxoptra.geocoding.geobase.webservice;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.WebEndpoint;
import javax.xml.ws.WebServiceClient;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;

/**
 * Project: Santa-cruz
 * Author:  Irina Isaeva
 * Created: 08.06.12 14:25
 * <p/>
 * Copyright (c) 1999-2012 Magenta Corporation Ltd. All Rights Reserved.
 * Magenta Technology proprietary and confidential.
 * Use is subject to license terms.
 * <p/>
 * $Id$
 */
@WebServiceClient(name = "GeoBaseWs", targetNamespace = "http://www.telogis.com/geobase/")
public class EchoGeoBaseWs extends Service {

    private static final String JBOSS_CONF_DIR_PROPERTY = "jboss.server.config.url";
    private static final String GEO_WSDL = "GeoBase.wsdl";

    private static final Object sync = new Object();

    private static EchoGeoBaseWs instance;
    private ObjectPool<GeoBaseWsSoap> pool;

    private static URL initWSDL() {
        try {
            return URI.create(getJBossConfPath() + "/" + GEO_WSDL).toURL();
        } catch (Exception e) {
            System.out.println(String.format("Failed to init GeoBase by url [%s]", getJBossConfPath() + "/" + GEO_WSDL));
            try {
                return URI.create("http://192.0.2.79/WebService/GeoBase.asmx?wsdl").toURL();
            } catch (MalformedURLException e2) {
                e2.printStackTrace();
                return null;
            }
        }
    }

    private static String getJBossConfPath() {
        String jbossConfDir = System.getProperty(JBOSS_CONF_DIR_PROPERTY);
        if (jbossConfDir != null) {
            jbossConfDir = jbossConfDir.replaceAll("\\\\", "/");
        }
        return jbossConfDir;
    }

    public static EchoGeoBaseWs getInstance() {
        if (instance == null) {
            synchronized (sync) {
                if (instance == null) {// if instance is still null
                    instance = new EchoGeoBaseWs(initWSDL(), new QName("http://www.telogis.com/geobase/", "GeoBaseWs"));
                }
            }
        }

        return instance;
    }

    public EchoGeoBaseWs(URL wsdlLocation, QName serviceName) {
        super(wsdlLocation, serviceName);
        initPool();
    }

    private void initPool() {
        pool = new ObjectPool<GeoBaseWsSoap>("GeoBaseWsSoap") {
            @Override
            protected GeoBaseWsSoap create() {
                return getPort(new QName("http://www.telogis.com/geobase/", "GeoBaseWsSoap"), GeoBaseWsSoap.class);
            }
        };
    }

    /**
     * @return returns GeoBaseWsSoap
     */
    @WebEndpoint(name = "GeoBaseWsSoap")
    public GeoBaseWsSoap getGeoBaseWsSoap() {
        return pool.checkOut();
    }

    public void finish(GeoBaseWsSoap proxy) {
        pool.checkIn(proxy);
    }

    public void invalidate(GeoBaseWsSoap proxy) {
        pool.invalidate(proxy);
    }


}
